//
//  ST_LiveP2pCollectionCell.swift
//  btvideo
//
//  Created by Jason on 2021/12/20.
//  Copyright © 2021 JarvanZhang. All rights reserved.
//

import UIKit
import SwiftyJSON
import SwiftyAttributes

class ST_LiveP2pCollectionCell: UICollectionViewCell {
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        contentView.addSubview(root_view)
        root_view.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        
        
        root_view.addSubview(cover_image_view)
        cover_image_view.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        
        root_view.addSubview(gradient_view)
        gradient_view.snp.makeConstraints { (make) in
            make.left.bottom.right.equalToSuperview()
            make.height.equalTo(76)
        }
        
        
        root_view.addSubview(onlineView)
        onlineView.snp.makeConstraints { (make) in
            make.right.equalToSuperview().offset(-10)
            make.top.equalToSuperview().offset(10)
            make.width.height.equalTo(TIANCompatibleValue(value: 10))
        }
        
        
        
        root_view.addSubview(type_label)
        type_label.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(7)
            make.height.equalTo(20)
            make.bottom.equalToSuperview().offset(-29)
        }
        
        root_view.addSubview(nickname_label)
        nickname_label.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(7)
            make.height.equalTo(20)
            make.bottom.equalToSuperview().offset(-7)
        }
        
        root_view.addSubview(hot_value_label)
        hot_value_label.snp.makeConstraints { make in
            make.right.equalToSuperview().offset(-8)
            make.height.equalTo(16)
            make.bottom.equalToSuperview().offset(-9)
        }
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    fileprivate lazy var onlineView = ST_OnlineStatusBallControl()
    
    fileprivate lazy var root_view:UIView = {
        let control = UIView()
        control.backgroundColor = .white
        control.cornerRadius = 10
          return control
      }()
    
    fileprivate lazy var cover_image_view:UIImageView = {
        let control = UIImageView()
        control.clipsToBounds = true
        control.contentMode = .scaleAspectFill
          return control
      }()
    
    private lazy var type_label_attributes : [Attribute] = [.font(UIFont.systemFont(ofSize: 10, weight: .regular)),.textColor(UIColor.white)]
    
    fileprivate lazy var type_label:ST_TianButton = {
        let l = ST_TianButton(style: .image_left_title_right(imageLeft: 5, imageSize: CGSize(width: 18, height: 18), layout: ST_TianButtonLayoutModel.inside_auto_resize(relevantOffset: 0, lastControlEdgeValue: -7)))
        l.backgroundColor = UIColor(hex: 0, transparency: 0.3)
        l.cornerRadius = 10
        l.borderWidth = 1
        l.borderColor = UIColor(hex: 0xFFFFFF, transparency: 0.5)
        l.isUserInteractionEnabled = false
        l.alpha = 0
        return l
    }()
    
    
    fileprivate lazy var nickname_label:UILabel = {
          let control = UILabel.init()
          control.text = ""
        control.textColor = UIColor.white
          control.font = UIFont.systemFont(ofSize: 14, weight: UIFont.Weight.medium)
          return control
      }()
    
    
    private lazy var hot_value_label_attributes : [Attribute] = [.font(UIFont.systemFont(ofSize: 12, weight: .medium)),.textColor(UIColor.white)]
    
    fileprivate lazy var hot_value_label:ST_TianButton = {
        let control = ST_TianButton(style: .image_left_title_right(imageLeft: 0, imageSize: CGSize(width: 8, height: 10), layout: .inside_auto_resize(relevantOffset: 4, lastControlEdgeValue: 0)))
        control.setImage(ST_ImageUtils.imageName("live_party_hot_value_icon"), for: .normal)
          return control
      }()
    
    
    fileprivate lazy var gradient_view:ST_FWGradientView = {
        
        let control = ST_FWGradientView()
        control.bgLayer.colors = [UIColor.init(hex: 0x1F1F1F, transparency: 0)!.cgColor, UIColor.init(white: 0, alpha: 0.3).cgColor]
        control.bgLayer.locations = [0, 1]
        control.bgLayer.startPoint = CGPoint(x: 0, y: 0)
        control.bgLayer.endPoint = CGPoint(x: 0, y: 1)
        
        return control
    }()
    
    
    
    
    weak var model:ST_LiveBeautyCellModel?{
        didSet{
            
            
            guard let m = model else { return  }
            
            
            if let coverUrl = self.model?.roomCover?.url,coverUrl.count > 2{
                
                self.cover_image_view.sd_setImage(with: coverUrl.compress_image().url, placeholderImage: ST_SwiftConst.shared.fetch_random_place_holder_image())
            }else{
                self.cover_image_view.sd_setImage(with: self.model?.avatar.compress_image().url, placeholderImage: ST_SwiftConst.shared.fetch_random_place_holder_image())
            }
            
            if m.themeType == 2{
                type_label.alpha = 1
                type_label.setImageUrl(m.themeImg.url, for: .normal)
                type_label.setTitle(m.themeName.withAttributes(type_label_attributes), for: .normal)
            }else{
                type_label.alpha = 0
            }
            
            
            nickname_label.text = m.nickname
            hot_value_label.setTitle(m.hot.string.withAttributes(hot_value_label_attributes), for: .normal)
            
            onlineView.status = model?.state == 3 ? 2 : 1
            
        }
    }
}


extension ST_LiveP2pCollectionCell:TianCellFollowPlayProtocol{
    
    func padding(playView: UIView) {
        
        root_view.insertSubview(playView, belowSubview: onlineView)
        playView.snp.makeConstraints { make in
            make.edges.equalTo(cover_image_view)
        }
    }
    
    
    
    
}





